var Newtouch = {
	version:'0.1'
};

//get web context 
Newtouch.context=(function(){
	var scripts = document.getElementsByTagName("script");
	var context = "";
	for(var i=0;i<scripts.length;i++){
		var script = scripts[i].getAttribute("src");
		if(script&&script.indexOf("res/js/newtouch-common.js"/*tpa=http://www.95590.cn/ebiz/res/js/res/js/newtouch-common.js*/)>-1){
			script = script.substring(1,script.length);
			context = "/"+script.substring(0,script.indexOf("/"));
			break;
		}
	}
	return context;
})();

/**
 * 使用方法参见：http://10.100.99.34:8080/browse/ESP-1391
 * author:kui.yang@newtouch.cn
 * 已知的bug，如果省份下拉框由该代码初始化时，有可能会导致通过IP地址无法判断地址
 */
;(function(Newtouch){
	//当前登录用户所在IP查询后的地址如：上海市
	var address = "";
	var province,city,state,success;
	
	/**
	 * 动态组装下拉框内容
	 * upperCode 上级代码
	 * level 当前级别
	 * selectId 下级下拉框ID，用于绑定change事件
	 * successCallback 当前下拉框change事件回调方法
	 */
	function buildDistrictList(upperCode,level,selectId,changeCallback,initValue){
		var head = "<option value=''>-请选择-</option>";
		if(!upperCode||upperCode==""){
			$("#"+selectId).html(head).change();
			return;
		}
		
		Newtouch.Ajax.sendParams({"upperCode":upperCode,"level":level},
				Newtouch.context+"/view/global/DistrictAction.jsp.searchDistrict",function(data){
			var html="";
			var i = 0;
			for(var v in data){
				i++;
				var object = data[v];
				var code = object["districtCode"];
				var name = object["districtName"];
				if((initValue==code)||(address!=""&&address.indexOf(name)>-1)){
					html+="<option value='"+code+"' selected='selected'>"+name+"</option>";
				}else{
					html+="<option value='"+code+"'>"+name+"</option>";
				}
			}
			// if(i!=1){
				html=head+html;
			// }
			changeCallback = changeCallback||function(){
				if(state["initValue"]!=null&&state["initValue"]!=""&&level=="2")
				buildDistrictList(initValue,"3",state["id"],city["change"],state["initValue"]);
			};
			$("#"+selectId).html(html).change(changeCallback());			
		},function(error){
			
		});
	}
	
	/**
	 * 动态组装下拉框内容
	 * upperCode 上级代码
	 * level 当前级别
	 * selectId 下级下拉框ID，用于绑定change事件
	 * successCallback 当前下拉框change事件回调方法
	 */
	function buildDistrictListNew(upperCode,level,selectId,changeCallback,initValue){
		changeCallback = changeCallback||function(){};
		var head = "<option value=''>-请选择-</option>";
		if(!upperCode||upperCode==""){
			$("#"+selectId).html(head).change();
			return;
		}
		
		Newtouch.Ajax.sendParams({"upperCode":upperCode,"level":level},
				Newtouch.context+"/view/global/DistrictAction.jsp.searchAreaCode",function(data){
			var html="";
			var i = 0;
			for(var v in data){
				i++;
				var object = data[v];
				var code = object["areaCode"];
				var name = object["areaName"];
				
				if((initValue==code)||(address!=""&&address.indexOf(name)>-1)){
					html+="<option value='"+code+"' selected='selected'>"+name+"</option>";
				}else{
					html+="<option value='"+code+"'>"+name+"</option>";
				}
			}
			if(i!=1){
				html=head+html;
			}
			$("#"+selectId).html(html).change(changeCallback());			
		},function(error){
			
		});
	}
	
	//省份下拉框change事件
	function provinceSelectChange(){
		var initValue = province["initValue"]||"";
		if(initValue!=""){
			$("#"+province["id"]).val(initValue);
		}
		var value = $(this).val();
		
		buildDistrictList(value,"2",city["id"],province["change"],city["initValue"]);
		province["initValue"]="";
		city["initValue"]="";
	}
	//城市下拉框change事件
	function citySelectChange(){
		var value = $(this).val();
		buildDistrictList(value,"3",state["id"],city["change"],state["initValue"]);
		state["initValue"]="";
	}
	
	//省份下拉框change事件
	function provinceSelectChangeNew(){
		var initValue = province["initValue"]||"";
		if(initValue!=""){
			$("#"+province["id"]).val(initValue);
		}
		var value = $(this).val();
		
		buildDistrictListNew(value,"2",city["id"],province["change"],city["initValue"]);
		province["initValue"]="";
		city["initValue"]="";
	}
	//城市下拉框change事件
	function citySelectChangeNew(){
		var value = $(this).val();
		buildDistrictListNew(value,"3",state["id"],city["change"],state["initValue"]);
		state["initValue"]="";
	}
	
	Newtouch.select = function(){};
	
	
	/**
	 * 主体方法，供外部调用使用
	 * option 为object
	 */
	Newtouch.select.load=function(option){
		 province = option["province"]||{};
		 city = option["city"]||{};
		 state = option["state"]||{};
		 var stateChange = state["change"]||function(){};
		 //省份下拉框是否初始化，默认为否
		 var provinceInit = province["init"]||false;
		 //是否按IP地址选中地区
		 var loadIP = option["loadIP"]||false;
		 if(provinceInit){
			 buildDistrictList(null,"1",city["id"],province["change"]); 
		 }
		$("#"+province["id"]).bind("change",provinceSelectChange).change();
		$("#"+city["id"]).bind("change",citySelectChange);
		$("#"+state["id"]).bind("change",stateChange);
		if(!loadIP){
			return ;
		}
		Newtouch.Ajax.sendParams({},Newtouch.context+"/view/global/DistrictAction.jsp.getUserLocal",function(data){
			
			address = data._value;
			try{
				$("#"+province["id"]).find("option").each(function(){
					var text = $(this).html();
						if(address.indexOf(text)>-1){
							$(this).attr("selected","selected");
							$("#"+province["id"]).change();
							return;
					}
				});
			}catch(e){
				
			}
		},function(error){
			if($&&$.logUtil){
				$.logUtil.save(error);
			}
		});
	}
	
	/**
	 * 主体方法，供外部调用使用
	 * option 为object
	 */
	Newtouch.select.loadNew=function(option){
		province = option["province"]||{};
		 city = option["city"]||{};
		 state = option["state"]||{};
		 var stateChange = state["change"]||function(){};
		 //省份下拉框是否初始化，默认为否
		 var provinceInit = province["init"]||false;
		 //是否按IP地址选中地区
		 var loadIP = option["loadIP"]||false;
		 if(provinceInit){
			 buildDistrictListNew(null,"1",city["id"],province["change"]); 
		 }
		$("#"+province["id"]).bind("change",provinceSelectChangeNew).change();
		$("#"+city["id"]).bind("change",citySelectChangeNew);
		$("#"+state["id"]).bind("change",stateChange);
		if(!loadIP){
			return ;
		}
		Newtouch.Ajax.sendParams({},Newtouch.context+"/view/global/DistrictAction.jsp.getUserLocal",function(data){
			
			address = data._value;
			try{
				$("#"+province["id"]).find("option").each(function(){
					var text = $(this).html();
						if(address.indexOf(text)>-1){
							$(this).attr("selected","selected");
							$("#"+province["id"]).change();
							return;
					}
				});
			}catch(e){
				
			}
		},function(error){
			if($&&$.logUtil){
				$.logUtil.save(error);
			}
		});
	}
})(Newtouch);



var i18nProperties = {
	version:'0.1',
	clear:'清除'
};

var LoadingModel = {
	html:'<div class="loading">Loading Data</div>'
};

//flag true/false
function showValidateMessage(input,message,flag){
	var msg = "";
	if(flag){
		msg = "<label class='fine' generated='true' for='"+input.attr("id")+"'>"+message+"</label>";
		input.removeClass("error");
	}else{
		msg = "<label class='error' generated='true' for='"+input.attr("id")+"'>"+message+"</label>";
		input.addClass("error");
	}
	input.parent().find("span.error label").replaceWith(msg);
}

	/**
	 * 参数 type :    1  允许输入数字   2  允许输入字母   3  允许输入数字和字母
	 */
	function keyInsertCheck(obj,type){
		var key = obj.value;
		if(type == 1){
			if(key.search("^-?\\d+(\\.\\d+)?$")!=0){
				key = key.replace(/[^x00-xff]/g,"");
				key = key.replace(/[a-zA-Z]/g,"");
        		obj.value = key;
        		return false;
    		}
		}else if(type == 2) {
			 if(key.search("^[a-zA-Z]+$")!=0){
         	 	key = key.replace(/[^x00-xff]/g,"");
         		key = key.replace(/[0-9]/g,"");
         		obj.value = key;
        		return false;
    	     }
		}else{
			 if(key.search("^[a-zA-Z0-9]+$")!=0){			 		
	         	key = key.replace(/[^x00-xff]/g,"");
	        	obj.value = key;
	        	return false;
    	 	 }
	    }					
	}

	function keyCheck(obj, allowNumber, allowUpperCase, allowLowerCase) {
		var key = obj.value;
		var essentialPattern = "";
		if (allowNumber) {
			essentialPattern = "0-9"
		}
		
		if (allowUpperCase) {
			essentialPattern = essentialPattern + "A-Z";
		}
		if (allowLowerCase) {
			essentialPattern = essentialPattern + "a-z";
		}

		
		if (key.search("^[" + essentialPattern + "]+$") != 0) {
			key = key.replace(/[^x00-xff]/g, "");
			if (!allowNumber) {
				key = key.replace(/[0-9]/g,"");
			}
			if (!allowLowerCase) {
				key = key.replace(/[a-z]/g,"");
			}
			if (!allowUpperCase) {
				key = key.replace(/[A-Z]/g,"");
			}
			obj.value = key;
			return false;
		}
		else {
			return true;
		}
	}

function showFakeProgress() {

	var metas = document.getElementsByTagName('META');
  	var themeName, appName;
  	for (var i = 0; i < metas.length; ++i) {
    	if (metas[i].name == "themeName") {
      		themeName = metas[i].getAttribute('CONTENT');
      	}
      	else if (metas[i].name == "appName") {
      		appName = metas[i].getAttribute('CONTENT');
      	}
    }
	
	if (document.getElementById("fakeProgessBar") == null) {
		var pdiv = document.createElement("DIV");
		pdiv = document.body.appendChild(pdiv);
		pdiv.id = "fakeProgessBar";
		var pImg = document.createElement("IMG");
		pImg.id = "fakeProgessBarImg";
		pImg = pdiv.appendChild(pImg);
		pdiv.style.zIndex = 1;
		pdiv.style.position = "absolute";
		pdiv.style.left = (document.body.clientWidth - 190)/2 + "px";
		pdiv.style.top = (document.body.clientHeight - 14)/2 + "px";
		pdiv.style.witdh = "190px";
		pdiv.style.height = "14px";
		var imgURL = "/" + appName + "/res/css/" + themeName + "/images/progressbar.gif";
		pImg.src = imgURL;
		pImg.end = "indefinite";		 
	}
	else {
		document.getElementById("fakeProgessBar").style.visibility = "visible";
	}
}

function hideFakeProgress() {
	
	if (document.getElementById("fakeProgessBar") == null) {
		return;
	}
	document.getElementById("fakeProgessBar").style.visibility = "hidden";
}

function showLoadingWindow(){
	var metas = document.getElementsByTagName('META');
  	var appName;
  	for (var i = 0; i < metas.length; ++i) {
    	if (metas[i].name == "appName") {
      		appName = metas[i].getAttribute('CONTENT');
      	}
    }
	var imgURL = "/" + appName + "/res/images/form/load.gif";
	var sWidth,sHeight;
    sWidth=document.body.offsetWidth;
    sHeight=document.body.offsetHeight;
    var bgObj=document.createElement("div");
    bgObj.setAttribute('id','bgDiv');
    bgObj.style.position="absolute";
    bgObj.style.top="0";
    bgObj.style.background="";
    bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
    bgObj.style.opacity="0.6";
    bgObj.style.left="0";
    bgObj.style.width=sWidth + "px";
    bgObj.style.height=sHeight + "px";
    bgObj.style.zIndex = "10000";
    document.body.appendChild(bgObj);

    var msgObj=document.createElement("div")
    msgObj.setAttribute("id","msgDiv");
    msgObj.setAttribute("align","center");
    msgObj.style.position = "absolute";
    msgObj.style.left = (document.body.clientWidth-200)/2 + "px";
	msgObj.style.top = (document.body.clientHeight-100)/2 + "px";
    msgObj.style.textAlign = "center";
    msgObj.style.zIndex = "10001";
    document.body.appendChild(msgObj);
    var img = document.createElement("img");
    img.src=imgURL;
    document.getElementById("msgDiv").appendChild(img);
    var txt=document.createElement("p");
    txt.innerHTML="LOADING...";
    txt.style.color="black";
    document.getElementById("msgDiv").appendChild(txt);
}

function closeLoadingWindow(){
	var v =document.getElementById("msgDiv");
	v.parentNode.removeChild(v);  
	var p =document.getElementById("bgDiv");
	p.parentNode.removeChild(p);   
}
function hiddenShow(id,entry,msg){
	if($('#'+id).css("display") == "none"){
		$('#'+id).show();
		$(entry).html("隐藏"+ msg +"输入");
		$(entry).removeClass("changeSpanOver");
		$(entry).addClass("changeSpan");
	} else {
		$('#'+id).hide();
		$(entry).html("显示"+ msg +"输入");
		$(entry).removeClass("changeSpan");
		$(entry).addClass("changeSpanOver");
	}
}
//父窗口打开
function windowParentOpen (url) {
	window.parent.location.href = url;
}